package com.qiyuexinxi.chat.service.impl;

import com.qiyuexinxi.chat.domain.Attention;
import com.qiyuexinxi.chat.domain.User;
import com.qiyuexinxi.chat.dto.request.*;
import com.qiyuexinxi.chat.dto.response.GetUserInfoResponse;
import com.qiyuexinxi.chat.exception.reg.exception.BizErrorCodeEnum;
import com.qiyuexinxi.chat.exception.reg.exception.BizException;
import com.qiyuexinxi.chat.mapper.AttentionMapper;
import com.qiyuexinxi.chat.mapper.UserMapper;
import com.qiyuexinxi.chat.service.AttentionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangxingyu
 * @date 2020/3/6
 * @description
 */
@Service
public class AttentionServiceImpl implements AttentionService {

    @Autowired
    AttentionMapper attentionMapper;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserMapper userMapper;


    @Override
    public boolean addAttention(AttentionAddRequest attentionAddRequest) {
        Attention attention = new Attention();
        BeanUtils.copyProperties(attentionAddRequest,attention);
        int i = attentionMapper.insertSelective(attention);
        if(i<=0){
            return false;
        }
        return true;
    }

    @Override
    public List<GetUserInfoResponse> getAttentionList(AttentionRequest attentionRequest) {
        List<Attention> attentions = attentionMapper.selectAttentionByUserId(attentionRequest.getUserId());
        List<Integer> attentionsUsers = new ArrayList<Integer>();
        for (Attention attention:attentions) {
            attentionsUsers.add(attention.getAttentionUser());
        }
        return getUserInfo(attentionsUsers);
    }

    @Override
    public List<GetUserInfoResponse> getFansList(AttentionRequest attentionRequest) {
        List<Attention> attentions = attentionMapper.selectByAttentionUser(attentionRequest.getUserId());
        List<Integer> userIds = new ArrayList<Integer>();
        for (Attention attention:attentions) {
            userIds.add(attention.getUserId());
        }
        return getUserInfo(userIds);
    }

    @Override
    public boolean deleteAttention(AttentionDeleteRequest attentionDeleteRequest) {
        Attention attention = new Attention();
        BeanUtils.copyProperties(attentionDeleteRequest,attention);
        attention.setStatus(0);
        int i = attentionMapper.updateByUserId(attention);
        if(i<=0){
            return false;
        }
        return true;
    }

    @Override
    public boolean isAttention(IsAttentionRequest isAttentionRequest) {
        User user = userMapper.selectByPrimaryKey(isAttentionRequest.getUserId());
        if(user==null || user.getDeleted()==0){
            throw  new BizException(BizErrorCodeEnum.USERID_NOT_EXIST);
        }
        User attentionUser = userMapper.selectByPrimaryKey(isAttentionRequest.getAttentionUserId());
        if(attentionUser==null || attentionUser.getDeleted()==0){
            throw  new BizException(BizErrorCodeEnum.ATTENTIONUSERID_NOT_EXIST);
        }
        int i = attentionMapper.selectCountAserIdAndAttentionUserid(isAttentionRequest.getUserId(), isAttentionRequest.getAttentionUserId());
        if(i>0){
            return true;
        }
        return false;
    }

    public List<GetUserInfoResponse> getUserInfo(List<Integer> userIds){
        if(userIds == null || userIds.size()==0){
            userIds = new ArrayList<Integer>();
            userIds.add(-1);
        }
        GetUserInfoRequest getUserInfoRequest = new GetUserInfoRequest();
        getUserInfoRequest.setUserIds(userIds);
        List<GetUserInfoResponse> userInfo = userService.getUserInfo(getUserInfoRequest);
        return userInfo;
    }

}
